package com.zlt.bkparking.controller.parking;

import com.zlt.bkparking.dto.ParkingDto;
import com.zlt.bkparking.dto.ParkingPriceDto;
import com.zlt.bkparking.entity.ParkingLot;
import com.zlt.bkparking.entity.ParkingLotExample;
import com.zlt.bkparking.service.ParkingService;
import com.zlt.bkparking.utils.JWTUtil;
import com.zlt.bkparking.utils.QnyUtil;
import com.zlt.bkparking.utils.StringUtil;
import com.zlt.bkparking.vo.ParkingLotVO;
import com.zlt.bkparking.vo.ParkingVo;
import com.zlt.bkparking.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("parking")
public class ParkingController {

    @Autowired
    private ParkingService parkingService;


    @GetMapping( "ParkingLots")
    public Result ParkingLots(){
        try {
            List<ParkingLotVO> parkingLotVOS=parkingService.ParkingLots();
            return (parkingLotVOS!=null) ? Result.ok("查询成功",parkingLotVOS):Result.error("查询失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    @PostMapping("photo")
    public Result ParkingUploadPhoto(@RequestParam("file") MultipartFile img){
        String path = null;
        if (img!=null){
            try {
                InputStream inputStream = img.getInputStream();
                // 文件名+文件后缀
                String submittedFileName = img.getOriginalFilename();
                // 有文件上传
                if (submittedFileName != null) {
                    String fileName = StringUtil.uuid() + submittedFileName.substring(submittedFileName.lastIndexOf(".")); //文件名（uuid）+ 文件后缀
                    path = QnyUtil.upload(inputStream, fileName);
                }
                return Result.ok("上传成功",path);
            } catch (IOException e) {
                e.printStackTrace();
                return Result.error("error");
            }
        }else {
            return Result.error("错误");
        }
    }



    //管理停车场价格
    @PutMapping("s_price")
    public Result setParkingPrice(@RequestBody ParkingPriceDto parkingPriceDto){
        // 数据校验
        System.out.println(parkingPriceDto);
        if (parkingPriceDto.getCaPrice()==null){
            return Result.error("价格不能为空");
        }
        if(parkingPriceDto.getPlId()==null){
            return Result.error("未选择停车场");
        }
        // 操作
        return parkingService.setPriceByPPD(parkingPriceDto)?Result.ok("修改成功"):Result.error("修改失败");
    }

    //录入停车场信息
    @PostMapping
    public Result addParking(@RequestHeader("token") String token,@RequestBody ParkingDto parkingDto){
        if (parkingDto.getPlName()==null){
            return Result.error("未输入名称");
        }
        if (parkingDto.getPlAddress()==null){
            return Result.error("未输入地址");
        }
        System.out.println(parkingDto);
        return parkingService.addParkingByDto(JWTUtil.getUsername(token),parkingDto)?Result.ok("成功"):Result.error("失败");
    }

    //显示停车场信息
    @GetMapping
    public Result getParkingInfo(Integer id){
        List<ParkingVo> parkingVos = null;
        if (id==null){
            parkingVos = parkingService.getInfo();
            return parkingVos.isEmpty()||parkingVos==null?Result.error("查询出错"):Result.ok("查询成功",parkingVos);
        }
        parkingVos = parkingService.getInfo(id);
        return parkingVos.isEmpty()||parkingVos==null?Result.error("查询出错"):Result.ok("查询成功",parkingVos.get(0));
    }

    //修改停车场信息->修改成功后返回停车场页面
    @PutMapping
    public Result setParkingInfo(@RequestBody ParkingDto parkingDto){
        //数据校验

        //处理
        return parkingService.setByDto(parkingDto)?Result.ok("修改成功"):Result.error("修改失败");
    }

    @GetMapping(value = "/del/{id}")
    public Result delParking(@PathVariable("id") Integer id){
        //数据校验
        System.out.println("AA"+id);
        //处理

        return parkingService.delByDto(id)?Result.ok("成功"):Result.error("失败");
    }






}
